
var whitespace = " \t\n\r";

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{  

	var i;
	// Is s empty?
	if (isEmpty(s)) return true;

	 // Search through string's characters one by one
	 // until we find a non-whitespace character.
	 // When we do, return false; if we don't, return true.
	 for (i = 0; i < s.length; i++)
	 {   
		 // Check that current character isn't whitespace.
		 var c = s.charAt(i);
 		
		 if (whitespace.indexOf(c) == -1) return false;
	 }
 
	 // All characters are whitespace.
	 return true;
}
function BuildStr(s, s1)
{
	  if (s.length> 0) s = s + "\n";
	  s = s + s1;
	  return s;
}

function isCharsInBag (s, bag)
{  
  var i;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
	  // Check that current character isn't whitespace.
	  var c = s.charAt(i);
	  if (bag.indexOf(c) == -1) return false;
  }
  return true;
}

function echeck(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return true;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return true;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return true;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return true;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return true;
	 }

	 return false;					
}


function isEmail(string) 
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

function validateUsername(loginName)
{
	//Validate the login name
	if (!isCharsInBag( loginName, "abcdefghijklmnopqrstuvwxyz0123456789.-_" ))
	{
		return "Login name has invalid characters.\n" ;
	}
	else if ( loginName.length < 3 )
	{
		return "Login name must be 3 or more characters.\n" ;
	}
	else
	{
		var count;
		for (count=0; count < loginName.length; count++)
		{
			if (loginName.charAt(count) == "_")
			{
				if (loginName.charAt(count+1) == "_")
				{
					return "Login name may not contain more than one consecutive underscore.\n";
				}
			}
		}
	}
	return "";
}


function monthno(mnth)
{
	getmonthno=13
	mnth=mnth.toLowerCase();
	switch(mnth)
	{
		case "jan": getmonthno = 1; break;
		case "feb": getmonthno = 2; break;
		case "mar": getmonthno = 3; break;
		case "apr": getmonthno = 4; break;
		case "may": getmonthno = 5; break;
		case "jun": getmonthno = 6; break;
		case "jul": getmonthno = 7; break;
		case "aug": getmonthno = 8; break;
		case "sep": getmonthno = 9; break;
		case "oct": getmonthno = 10; break;
		case "nov": getmonthno = 11; break;
		case "dec": getmonthno = 12; break;       
	}
	return getmonthno;				
}

function monthname(mnth)
{
	getmonthname=13
	mnth=mnth;
	switch(mnth)
	{
		case "01": getmonthname= "jan"; break;
		case "02": getmonthname= "feb"; break;
		case "03": getmonthname= "mar"; break;
		case "04": getmonthname= "apr"; break;
		case "05": getmonthname= "may"; break;
		case "06": getmonthname= "jun"; break;
		case "07": getmonthname= "jul"; break;
		case "08": getmonthname= "aug"; break;
		case "09": getmonthname= "sep"; break;
		case "10": getmonthname= "oct"; break;
		case "11": getmonthname= "nov"; break;
		case "12": getmonthname= "dec"; break;       
	}
	return getmonthname;				
}

function checkDates(paramStartDate, paramEndDate)
{
	
	dtfrom=paramStartDate;

	if(dtfrom!="")
	{
		dt1_s=dtfrom.split("-");
		dt1_d=dt1_s[0]	
		dt1_m=dt1_s[1]
		dt1_y=dt1_s[2]		
	}
	
	dtto=paramEndDate;

	if(dtto!="")
	{
		dt2_s=dtto.split("-");
		dt2_d=dt2_s[0]	
		dt2_m=dt2_s[1]
		dt2_y=dt2_s[2]		
	}
		
	
	var dteStart = new Date(dt1_y, dt1_m, dt1_d,0,0,0);
	var dteEnd = new Date(dt2_y, dt2_m, dt2_d,0,0,0);

	var dteCurrent = new Date();	

	if(dteStart.valueOf() - dteEnd.valueOf() > 0)
	{
	   	getvalno=0
	}
	else
	{
		getvalno=1;
	}
	return getvalno;				
}

function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function add_zeros(num, zeros) 
{
	// It will add trailing zeros
	if(zeros == undefined || zeros == null)
		zeros = 2;
		
	var result = num.toFixed(zeros); // result will equal 10.00
	return result;
}

function checknumeric(thefval,thestr)
{
	if(!isCharsInBag(thefval, thestr) || isNaN(thefval))
		{
			alert("Please enter numeric value");
			return false;
		}
}

function checksort(val)
{	
	if(isNaN(val))
	{
		alert("Please enter numeric value \n");
		this.focus();
	}
}

//validate allowed imagetype
function checkImageType(sImageName)
{	
	var imageFilename = trimAll(sImageName);	
	var imageExtention = "";
	var arrAllowedImageType = new Array();
	arrAllowedImageType[0] = "jpg";
	arrAllowedImageType[1] = "jpeg";
	arrAllowedImageType[2] = "png";
	
	//get image file TYPE
	var arrImageName = new Array();		
	arrImageName = imageFilename.split(".");		
	imageExtention = arrImageName[arrImageName.length-1];
	imageExtention = imageExtention.toLowerCase();
	var i;
	for (i=0; i < arrAllowedImageType.length; i++) 
	{
		if (arrAllowedImageType[i] == imageExtention) 
		{
			return true;
		}
	}
	return false;
}


//validate allowed imagetype
function checkLogoType(sImageName)
{	
	var imageFilename = trimAll(sImageName);	
	var imageExtention = "";
	var arrAllowedImageType = new Array();
	arrAllowedImageType[0] = "jpg";
	arrAllowedImageType[1] = "jpeg";
	arrAllowedImageType[2] = "gif";
	
	//get image file TYPE
	var arrImageName = new Array();		
	arrImageName = imageFilename.split(".");		
	imageExtention = arrImageName[arrImageName.length-1];
	imageExtention = imageExtention.toLowerCase();
	
	var i;
	for (i=0; i < arrAllowedImageType.length; i++) 
	{
		if (arrAllowedImageType[i] == imageExtention) 
		{
			return true;
		}
	}
	return false;
}

/* Increase textarea rows as user puts content - Toreduse overall page height so make userfriedly */
function textAreaRowsHeight(field,linelimit,textmaxlimit) 
{
	//alert(field);
	//alert(field.value.length);
	textCounter(field,textmaxlimit);
	LineHeight =20;
	if (field.value.length > linelimit) // if too long...trim it!
	{
		var rows_req=Math.ceil(field.value.length/linelimit);
		LineHeight -= rows_req;
		var height_req=parseInt(rows_req)*LineHeight;
		field.style.height = height_req+"px;";
		field.rows = rows_req;		
	}
	else
	{
		field.style.height = "20px;";
		field.rows = 1;
	}
}

/* checks max character length for textarea */
function textCounter(field, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		alert("You can not enter more than "+maxlimit+" characters");
		field.value = field.value.substring(0, maxlimit);
	}
}

function trimAll(sString) 
{
	if(!isEmpty(sString))
	{
		while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '\n' || sString.substring(0,1) == '\r' || sString.substring(0,1) == '\t')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ' || sString.substring(sString.length-1, sString.length) == '\n' || sString.substring(sString.length-1, sString.length) == '\r' || sString.substring(sString.length-1, sString.length) == '\t')
		{
			sString = sString.substring(0,sString.length-1);
		}
	}
	return sString; 
}

function doAdminPost(page,theForm) //for paging script
{ 	
	document.all.page.value = page;
	theForm.submit();
}

function doAdminSearch()
{
	//alert(document.all.letter);	
	if(document.all.letter != undefined)
		document.all.letter.value='';
	if(document.all.page != undefined)
		document.all.page.value=1;		
	if(document.all.search != undefined)
		if(document.all.search.value == 'Enter Keyword') { document.all.search.value='';}
	
	document.all.page.value = 1;
}

function showpopup(val,filedName) //Date POPup
{
	popUpCalendar(val, filedName, "mm/dd/yyyy");
}

function doProductList(page,theForm) //for paging script
{ 	
	theForm.method = 'GET';
	//theForm.event_page.value = event_page;
	//if(document.all.page != 'undefined')	document.all.page.value = page;
	theForm.submit();
}
function doPriceSort(sortField) //for paging script
{ 	
	theForm = document.form1;
	theForm.ps.value = sortField.value;
	theForm.method = 'GET';
	theForm.submit();
}
function doSearchFilter(sortField,form_name) //for product listing sort script
{	
	theForm = document.getElementById(form_name);
	if(isNaN(sortField))
		var sortVal = document.getElementById(sortField).value;
	else
		var sortVal = sortField;

	theForm.ps.value = sortVal;
	theForm.method = 'get';
	theForm.submit();
}

function ShowMeEverithingSearch(sortField, filterField) //for product listing sort script
{	
	theForm = document.form1;
	//theForm = document.getElementById(form_name);
	theForm.ps.value = document.getElementById(sortField).value;
	theForm.ps_filter.value = document.getElementById(filterField).value;
	theForm.method = 'get';
	theForm.submit();
}
function validateSearch(theForm)
{
	theForm.q.value = document.all('txtsearch').value;
	if(isWhitespace(theForm.q.value) || theForm.q.value == "Enter Keyword or Product Code")
	{
		theForm.q.value = "";
		theForm.q.focus();
	}
	return true;
}

//IT AUTO SELECTS VALUE OF DROPDOWN BASED ON SEARCH VALUE PASSED TO IT
//IT COMPARES DROPDOWN VALUE AND NOT TEST
function selectdatabasevalue(toselect,cmb)
{
	for(i=0;i<eval(cmb).length;i++)
	{
		if(eval(cmb).options[i].value==toselect)
		{
			eval(cmb).selectedIndex = i;
		}
	}
}

function URLEncode(objSource)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = objSource.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '" 
						+ ch 
						+ "' cannot be encoded using standard URL encoding.\n" +
						  "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//objDest.value = encoded;
	return encoded;
}


/* DATE VALIDATION SCRIPTS --- START --- */
// Declaring valid date character, minimum year and maximum year
//it validates date in MM/DD/YYYY Format
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,dtSep){
	var daysInMonth = DaysArray(12)
	
	if(dtSep != null)
		if(dtSep.length > 0) dtCh = dtSep;	
	
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//The date format should be : mm/dd/yyyy
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		//Please enter a valid month
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//Please enter a valid day"
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//Please enter a valid 4 digit year between "+minYear+" and "+maxYear
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//Please enter a valid date
		return false;
	}
return true
}

/* DATE VALIDATION SCRIPTS --- END --- */


var zzz=0;
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
	if(zzz==1) { opacStart=30; opacEnd=100; }

	var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
	zzz++;	if(zzz==2) zzz=0;
	//alert(zzz);
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 